import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# 定义模型名称和对应的性能指标
models = {
    'XGBOOST': {'Accuracy': 0.812, 'MSE': 91.3},
    'RandomForest & DecisionTree Voting': {'Accuracy': 0.9066, 'MSE': 63.3},
    'SVM': {'Accuracy': 0.893, 'MSE': 72.1},
    'DecisionTree': {'Accuracy': 0.8943, 'MSE': 66.1}
}

# 创建数据框
df_performance = pd.DataFrame(models).T.reset_index()
df_performance.columns = ['Model', 'Accuracy', 'MSE']

# 打印数据框
print(df_performance)

# 生成可视化图表
plt.figure(figsize=(14, 6))

# 绘制准确度
plt.subplot(1, 2, 1)
ax1 = sns.barplot(x='Model', y='Accuracy', data=df_performance, palette='viridis')
plt.title('Accuracy comparison')
plt.ylim(0, 1)
plt.xticks(rotation=45)

# 在条形图上添加数值标签
for i, v in enumerate(df_performance['Accuracy']):
    ax1.text(i, v + 0.01, f'{v:.4f}', ha='center', va='bottom')

# 绘制均方误差
plt.subplot(1, 2, 2)
ax2 = sns.barplot(x='Model', y='MSE', data=df_performance, palette='plasma')
plt.title('MSE comparison')
plt.xticks(rotation=45)

# 在条形图上添加数值标签
for i, v in enumerate(df_performance['MSE']):
    ax2.text(i, v + 1, f'{v:.1f}', ha='center', va='bottom')

plt.tight_layout()
plt.show()
